var storagePath;
var pathCreate = "/alopex/createNewFile";
var pathCopy = "/alopex/copy";
var pathMakeDirs = "/alopex/mkdir1/mkdir2";
var pathRename = "/alopex/rename";
var pathMove = "/alopex/move";

function init() {
	_initHeader();
	
	var array = new Array();
	
	array.push("createNewFile");
	array.push("exists");
	array.push("copy");
	array.push("deleteFile");
	array.push("mkdirs");
	array.push("isDirectory");
	array.push("rename");
	array.push("move");
	array.push("getStoragePath");
	
	draw_list(array);
	
	file.getStoragePath(choiceStorageCallback, false);
}

function _initHeader() {
	$("#titleBarBackButton").click(backButtonHandler);
	$("#titleBarHomeButton").click(homeButtonHandler);
}

function draw_list(list) {
	
	var outputHtml = "";
	
	$.each(list, function(idx, value) {
		outputHtml = "";
		outputHtml += "<li id=\"btn_row_" + idx + "\"><a href=\"#\">" + value + "</a></li>";
		$("#listview").append(outputHtml);
		$("#btn_row_" + idx).click(function(e) {
			setOnListClickHandler(idx);
		});
	});
	$("#listview").listview("refresh");
}

function setOnListClickHandler(idx) {
	switch(idx) {
		case 0 :
			var path = storagePath + pathCreate;
			
			file.createNewFile(path, fileCallback);
		break;
		
		case 1 :
			var to = storagePath + pathCreate;
			
			file.exists(to, existCallback);
			
		break;
		
		case 2 :
			var from = storagePath + pathCreate;
			var to = storagePath + pathCopy;
			
			file.copy(from, to, fileCallback);
		break;
		
		case 3 :
			var to = storagePath + pathCopy;
			
			file.deleteFile(to, fileCallback);
		break;
		
		case 4 :
			var path = storagePath + pathMakeDirs;
			
			file.mkdirs(path, fileCallback);
		break;
		
		case 5 :
			var path = storagePath + pathMakeDirs;
			// var path = storagePath + "/alopex";
			
			file.isDirectory(path, isDirectoryCallback);
		break;
		
		case 6 :
			var path = storagePath + pathCreate;
			var name = "rename";
			
			file.rename(path, name, fileCallback);
		break;
		
		case 7 :
			var from = storagePath + pathRename;
			var to = storagePath + pathMove;
			
			file.move(from, to, fileCallback);
		break;
		
		case 8 :
			file.getStoragePath(getStoragePathCallback, true);
		break;
		
		default :
		break;
	}
}

function getStoragePathCallback(result) {
	alert(result);
}

function choiceStorageCallback(result) {
	storagePath = result;
}

function fileCallback(result) {
	if(result)
		alert("success");
	else
		alert("fail");
}

function isDirectoryCallback(result) {
	if(result)
		alert("directory");
	else
		alert("not directory");
}

function existCallback(result) {
	if(result)
		alert("exist");
	else
		alert("not exist");
}